#include "globals.h"
#include "debug/debug.h"
#include "utils.h"
#include <exploit_config.h>

tGlobals * get_globals() 
{
	return (tGlobals *) GLOBALS_ADDR;
}

void init_globals()
{
	if ((u32)GLOBALS_ADDR < 0x14000)
	{
		if (HBL_STUBS_START + NUM_HBL_IMPORTS*2*sizeof(u32) > (u32)GLOBALS_ADDR)
		{
			exit_with_log("FATAL: Increase GLOBALS_ADDR", NULL, 0);
		}

		u32 size = sizeof(tGlobals); 
		if (size + (u32)GLOBALS_ADDR >= 0x14000)
		{
			logstr1("Globals structure doesn't fit in Scratchpad RAM: 0x%08lX\n", size);
			exit_with_log("FATAL: Globals too big", NULL, 0);
		}
	}
	tGlobals * g = get_globals();
	memset(g, 0, sizeof(tGlobals));
	strcpy(g->menupath, HBL_ROOT HBL_MENU);	

	g->syscalls_from_p5 = 1; // Always available

	g->exit_callback_called = 0;

	g->curr_channel_id = -1;

	reset_vita_dirs();
}

void reset_vita_dirs()
{
	tGlobals * g = get_globals();
	int i,j;

	for (i=0;i<MAX_OPEN_DIR_VITA;++i)
		for (j=0;j<2;++j)
			g->directoryFix[i][j] = -1;
}
